package com.opennetexam.manager.sqlprovider;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 奶茶
 * Created on 2017/11/8.
 */
public class ExamineeOperateLogProvider {

    private static Logger logger = Logger.getLogger(ExamineeOperateLogProvider.class);

    /**
     * 条件查询学生操作日志
     * @param parameter
     * @return
     */
    public String findExamineeOperateLogBySomeWhere(Map<String,Object> parameter){
        StringBuilder stringBuffer = new StringBuilder("SELECT x.LogID,x.AppID,x.ExamineeID,y.ExamineeName,x.OptIPAddress,x.OptTime,x.OptDescription,x.SessionID FROM (");
        stringBuffer.append(" SELECT * FROM ExamineeOperateLog WHERE 1 = 1");
        if(parameter.get("appId") != null && !("").equals(parameter.get("appId"))){
            stringBuffer.append(" AND AppID LIKE \'%").append(parameter.get("appId")).append("%\'");
        }
        if(parameter.get("examineeId") != null && !("").equals(parameter.get("examineeId"))){
            stringBuffer.append(" AND ExamineeID LIKE \'%").append(parameter.get("examineeId")).append("%\'");
        }
        if(parameter.get("ipAddress") != null && !("").equals(parameter.get("ipAddress"))){
            stringBuffer.append(" AND OptIPAddress LIKE \'%").append(parameter.get("ipAddress")).append("%\'");
        }
        if(parameter.get("beginTime") != null && !("").equals(parameter.get("beginTime"))){
            stringBuffer.append(" AND OptTime >= \'").append(parameter.get("beginTime")).append("\'");
        }
        if(parameter.get("endTime") != null && !("").equals(parameter.get("endTime"))){
            stringBuffer.append(" AND OptTime <= \'").append(parameter.get("endTime")).append("\'");
        }
        if(parameter.get("description") != null && !("").equals(parameter.get("description"))){
            stringBuffer.append(" AND OptDescription LIKE \'%").append(parameter.get("description")).append("%\'");
        }
        if(parameter.get("sessionId") != null && !("").equals(parameter.get("sessionId"))){
            stringBuffer.append(" AND SessionID LIKE \'%").append(parameter.get("sessionId")).append("%\'");
        }

        stringBuffer.append(" ) AS x JOIN ( ");
        if(parameter.get("examineeName") != null){
            stringBuffer.append("SELECT * FROM Examinee WHERE ExamineeName LIKE \'%").append(parameter.get("examineeName")).append("%\'");
        }else{
            stringBuffer.append("SELECT * FROM Examinee ");
        }
        stringBuffer.append(" ) AS y ON x.ExamineeID = y.ExamineeID ");

        logger.info(stringBuffer.toString());
        return stringBuffer.toString();
    }

    public static void main(String[] args){
        Map<String,Object> map = new HashMap<>();
        map.put("appId","123");
        map.put("examineeId",123);
        map.put("ipAddress","123");
        map.put("beginTime","123");
        map.put("endTime","123");
        map.put("description","123");
        map.put("sessionId","123");
        map.put("examineeName","123");

//        String examineeOperateLogBySomeWhere = findExamineeOperateLogBySomeWhere(map);
//        System.out.println(examineeOperateLogBySomeWhere);
    }
}
